#include "../include/play.h"

Play::Play()
{
    Init();
    stayLoop = true;
    direction = NONE;
    keystate = NULL;
    font = TTF_OpenFont("comic.ttf",12);
    color.r=0;
    color.g=0;
    color.b=255;

    gobel = new Hero();
    tugla = new Tugla();

    tugla2 = new Tugla();
    tugla2->setSpriteImage("img/tugla2.png");
    tugla2->setX(500);
    tugla2->setY(350);

    itRender = vRender.begin();
    vRender.push_back(gobel);
    vRender.push_back(tugla);
    vRender.push_back(tugla2);

    gobel->sendTugla(tugla2);
}

Play::~Play()
{
    TTF_CloseFont(font);
    SDL_FreeSurface(screen);
}

void Play::Execute()
{
    SDL_EnableKeyRepeat(10, 10);
    while(stayLoop)
    {
        getEvents();
        RenderAll();
    }
}

void Play::getEvents()
{
    SDL_Event event;
    if (SDL_PollEvent(&event))
    {
        switch (event.type)
        {
            case SDL_QUIT:
                stayLoop = false;
                break;
            case SDL_KEYDOWN:
                    if(event.key.keysym.sym == SDLK_ESCAPE)
                        stayLoop = false;
                break;
//            case SDL_MOUSEMOTION:
//                gobel->setX(event.motion.x);
//                if(!gobel->CheckCollision(gobel,tugla2))
//                {
//                    gobel->setY(event.motion.y);
//                }
//                break;
        }
    }

    keystate = SDL_GetKeyState(NULL);

    //Gobelin koordinatları
    echo(font,"Gobel X:",5,5,color);
    echo(font,gobel->getX(),50,5,color);
    echo(font,"Gobel Y:",5,20,color);
    echo(font,gobel->getY(),50,20,color);
    ////////////////////////////////////



    if (keystate[SDLK_LEFT] )
    {
        direction = LEFT;
        gobel->setWalkState(true);
    }
    if (keystate[SDLK_RIGHT] )
    {
        direction = RIGHT;
        gobel->setWalkState(true);
    }
    if (event.key.keysym.sym == SDLK_SPACE)
    {
        direction = JUMP;
        gobel->setJumpState(true);
    }

    gobel->setDirection(direction);

}
int Play::Init()
{
    if (SDL_Init(SDL_INIT_VIDEO) < 0)
        exit(11);

    screen = SDL_SetVideoMode(800, 600, 32,SDL_DOUBLEBUF|SDL_RESIZABLE);
//    screen = SDL_SetVideoMode(800, 600, 32,SDL_DOUBLEBUF|SDL_FULLSCREEN);
    if(!screen)
        exit(12);

    if(TTF_Init()==-1)
        exit(13);
}

void Play::RenderAll()
{
//    for (itRender = vRender.begin();itRender!=vRender.end();itRender++)
//    {
//        SDL_FillRect(screen,NULL,0);
//        (*itRender)->Logic();
//        (*itRender)->Render(screen);
//        SDL_Flip(screen);
//    }

    gobel->Render(screen);
    gobel->Logic();

    tugla->Render(screen);
    tugla->Logic();

    tugla2->Render(screen);
    tugla2->Logic();

    SDL_Flip(screen);
    SDL_FillRect(screen,NULL,0);
}

void Play::echo(TTF_Font* _font,char* _string,int x,int y,SDL_Color _color)
{
    SDL_Surface* string;
    SDL_Rect _bounce = {x,y,0,0};

    string  = TTF_RenderText_Solid(_font,_string,_color);
    SDL_BlitSurface(string,0,screen,&_bounce);

    SDL_FreeSurface(string);
}

void Play::echo(TTF_Font* _font,int _sayi,int x,int y,SDL_Color _color)
{
    SDL_Rect _bounce = {x,y,0,0};
    std::stringstream temp;
    std::string _string;
    SDL_Surface* string;

    temp<<_sayi;
    temp>>_string;

    string  = TTF_RenderText_Solid(_font,_string.c_str(),_color);
    SDL_BlitSurface(string,0,screen,&_bounce);

    SDL_FreeSurface(string);
}
